/**
 * Project: topdt-jbpm Copyright 2013 Top Digital Technology Co. Ltd. All right reserved.
 */
package com.topdt.jbpm.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.PaginationDao;
import com.thx.common.util.AppContext;
import com.topdt.jbpm.model.Field;

/**
 * 建立日期 : 2013-4-25 下午12:03:29<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
@Repository
public class FieldDao extends PaginationDao<Field> {

  @SuppressWarnings("unchecked")
  public List<Field> getFieldsByformId(String formId) {
    String hql = "from Field where formId =? order by seqenceNum";
    List<Field> list = createQuery(hql, new Object[] {formId}).list();
    return list;
  }

  /**
   *
   * @author _zili
   * @Description:分页查询方法
   * @date 2015年4月1日 上午11:43:58
   */
  public List<Field> getAllByFormId(String formId) {
    List<Object> params = new ArrayList<Object>();
    String hql = "from Field where formId =? order by seqenceNum";
    params.add(formId);
    return findPageList(hql.toString(), params.toArray());
  }

  /**
   * 查询字段列表页 建立日期 : 2013-6-20 <br>
   * 作者 : 武海峰<br>
   * 
   * @param field
   * @param start
   * @param limit
   * @return
   */
  @SuppressWarnings("unchecked")
  public List<Field> queryAllByCondition(Field field, final int start, final int limit,
      String formId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "companyId='" + comId + "' and ";
    String hql = "from Field f where " + condition + "f.formId = ?  order by f.seqenceNum";
    List<Field> listField =
        createQuery(hql, new Object[] {formId}).setFirstResult(start).setMaxResults(limit).list();
    if (listField.size() > 0) {
      return listField;
    } else {
      return null;
    }
  }

  /**
   * 查询字段列表页数 建立日期 : 2013-6-20 <br>
   * 作者 : 武海峰<br>
   * 
   * @param field
   * @return
   */
  public int queryCount(Field field, String formId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "companyId='" + comId + "' and ";
    String hql = "select count(*) from Field f where " + condition + " f.formId = '" + formId + "'";
    Object result = createQuery(hql).uniqueResult();
    if (result != null) {
      return new Integer(result.toString());
    }
    return 0;
  }

  /**
   * 查询字段列表信息 建立日期 : 2013-6-20 <br>
   * 作者 : 武海峰<br>
   * 
   * @param field
   * @return
   */
  @SuppressWarnings("unchecked")
  public Field findById(String fieldId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "companyId='" + comId + "' and ";
    String hql = "from Field f where " + condition + "f.fieldId = '" + fieldId + "'";
    List<Field> list = createQuery(hql).list();
    if (list.size() > 0) {
      return (Field) createQuery(hql).list().get(0);
    }
    return null;
  }

  @SuppressWarnings("unchecked")
  public int findCount(String formId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "companyId='" + comId + "' and ";
    String sql =
        "select seqence_num from wf_field where " + condition
            + "form_id=? order by to_number(seqence_num) desc";
    List<Field> list = createSqlQuery(sql, formId).list();
    if (list.size() > 0) {
      return Integer.parseInt(list.get(0).toString());
    }
    return 0;
  }

  @SuppressWarnings("unchecked")
  public Field findByFieldText(String fieldText, String formId) {
    String hql = "from Field f where f.fieldText = ? and formId = ?";
    List<Field> list = createQuery(hql, new Object[] {fieldText, formId}).list();
    if (list.size() > 0) {
      return (Field) list.get(0);
    }
    return null;
  }

  @SuppressWarnings("unchecked")
  public Field findByFieldName(String fieldName, String formId) {
    String hql = "from Field f where f.fieldName = ? and formId = ?";
    List<Field> list = createQuery(hql, new Object[] {fieldName, formId}).list();
    if (list.size() > 0) {
      return (Field) list.get(0);
    }
    return null;
  }

  @SuppressWarnings("unchecked")
  public List<Field> getShowFieldsByformId(String formId) {
    String hql = "from Field where formId =? and isShow='Y' order by seqenceNum";
    List<Field> list = createQuery(hql, new Object[] {formId}).list();
    return list;
  }

  @SuppressWarnings("unchecked")
  public Field getFieldByFieldNameAndFormId(String key, String formId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "companyId='" + comId + "' and ";
    String hql =
        "from Field where " + condition
            + "formId =? and isShow='Y' and fieldName=? order by seqenceNum";
    List<Field> list = createQuery(hql, new Object[] {formId, key}).list();
    if (list != null && list.size() > 0)
      return list.get(0);
    return null;
  }

  /**
   * 获取field模板数据并插入表里
   * 
   * @param companyId
   */
  public void addField(String companyId) throws HibernateException {
    SQLQuery sql =
        (SQLQuery) getSession()
            .createSQLQuery(
                "insert into wf_field w(w.FIELD_ID,w.FIELD_TEXT,w.DEFAULT_VALUE,w.FIELD_NAME,w.FIELD_OPTION,w.FORM_ID,w.FIELD_TYPE,w.FIELD_LENGTH,w.IS_NECESSARY,w.IS_SHOW,w.SEQENCE_NUM,w.COMPANY_ID) "
                    + " select :companyId||sw.FIELD_ID,sw.FIELD_TEXT,sw.DEFAULT_VALUE,sw.FIELD_NAME,sw.FIELD_OPTION,:companyId||sw.FORM_ID,sw.FIELD_TYPE,sw.FIELD_LENGTH,sw.IS_NECESSARY,sw.IS_SHOW,sw.SEQENCE_NUM,:companyId"
                    + " from sys_wf_field sw").setString("companyId", companyId);
    sql.executeUpdate();
  }
}
